package com.sentiment.algorithm;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;

public class KmpCore {

	private String patternFileName;
	private String[] artics;
	private ArrayList<RecordBean> recordBeans = new ArrayList<RecordBean>();

	public KmpCore() {

	}

	public String[] getArtics() {
		return artics;
	}

	public void setArtics(String[] artics) {
		this.artics = artics;
	}

	public String getPatternFileName() {
		return patternFileName;
	}

	public void setPatternFileName(String patternFileName) {
		this.patternFileName = patternFileName;
	}

	public void showSimi() {
		for (RecordBean recordBean : recordBeans) {
			System.out.println(recordBean.toFile());
		}
	}

	public void writeToFileSimi(String filePath) {
		try {
			PrintWriter output = new PrintWriter(new BufferedWriter(new FileWriter(filePath)));
			for (RecordBean recordBean : recordBeans) {
				output.println(recordBean.toPattern());
			}
			output.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// public static void main(String[] args) {
	// args = new String[1];
	// args[0] = "C:\\Users\\asus\\workspace\\Detection1.1\\src\\minilin.xml";
	// KmpCore mini = new KmpCore();
	// LoadConfigurationXMLFile loadConfigurationXMLFile = new
	// LoadConfigurationXMLFile(args[0]);
	// loadConfigurationXMLFile.loadConfigurationXML();
	// mini.setPatternFileName(ArgumentConf.PATTERNPATH);
	// SelfFile selfFile = new SelfFile();
	//
	// mini.setArtics(selfFile.ShowDirListPath(ArgumentConf.articlesFilePath));
	// mini.kmpJob();
	// mini.showSimi();
	// mini.writeToFileSimi(ArgumentConf.similaroutputPath);
	//
	// }

	public void kmpJob() {
		String regex = "^[\\u0020\\u3000]*$";
		SelfFile patternFile = new SelfFile(patternFileName);
		SelfFile articleFile;
		int patt = 0;
		while (patternFile.getpatternPara()) {

			String pattern_text = patternFile.getText().replaceAll(regex, "");

			if (pattern_text.trim().isEmpty()) {
				patt++;
				continue;

			}
			int art = 0;
			while (art < artics.length) {
				articleFile = new SelfFile(artics[art]);
				int tag = 0;
				while (articleFile.getpatternPara()) {
					String article_text = articleFile.getText().replaceAll(regex, "");
					if (article_text.trim().isEmpty()) {
						tag++;
						continue;
					}
					KmpMethod kmpMethod = new KmpMethod(pattern_text, patt, article_text, tag);
					kmpMethod.selfPattern();
					ArrayList<KmpMethod.Recoed> arrayList = kmpMethod.getRecoeds();
					for (KmpMethod.Recoed recoed : arrayList) {
						String[] strings = artics[art].split("/");
						String arti_name = strings[strings.length - 1];
						RecordBean recordBean = new RecordBean(patt, recoed.getPoint(), arti_name, tag, recoed.getTag(),
								recoed.getSimilar());
						recordBeans.add(recordBean);
					}
					tag++;
				}
				art++;
			}
			patt++;
		}
	}
}
